/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa036is.daoimplementation;

import cz.muni.fi.pa036is.daointerface.CommentDAO;
import cz.muni.fi.pa036is.entity.Comment;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.springframework.stereotype.Repository;

/**
 *
 * @author Kuba
 */
@Repository
public class CommentDAOImpl implements CommentDAO {

    private Connection con = null;
    private Statement st = null;
    private ResultSet rs = null;

    public CommentDAOImpl() throws ClassNotFoundException {

        String url = "jdbc:postgresql://localhost/PA036";
        String user = "postgres";
        String password = "test";


        try {
												Class.forName("org.postgresql.Driver");
            con = DriverManager.getConnection(url, user, password);
        } catch (SQLException ex) {
            Logger.getLogger(CommentDAOImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void addComment(Comment comment) {
        try {


            String insert = "INSERT INTO comment"
                    + "(match_id, name, text, date) VALUES"
                    + "(?,?,?,?)";
            PreparedStatement preparedStatement = con.prepareStatement(insert);




            preparedStatement.setLong(1, comment.getMatchId());
            preparedStatement.setString(2, comment.getName());
            preparedStatement.setString(3, comment.getText());
            preparedStatement.setTimestamp(4, comment.getDate());


            preparedStatement.execute();


        } catch (SQLException ex) {
            Logger.getLogger(CommentDAOImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void editComment(Comment comment) {
        try {


            String update = "UPDATE comment SET match_id= ?, name= ?, text= ?, date= ? WHERE id= ?";


            PreparedStatement preparedStatement = con.prepareStatement(update);


            preparedStatement.setLong(1, comment.getMatchId());
            preparedStatement.setString(2, comment.getName());
            preparedStatement.setString(3, comment.getText());
            preparedStatement.setTimestamp(4, comment.getDate());
            preparedStatement.setLong(5, comment.getId());


            preparedStatement.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(CommentDAOImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void deleteComment(Long id) {
        try {


            String delete = "DELETE FROM comment WHERE id=?";


            PreparedStatement preparedStatement = con.prepareStatement(delete);


            preparedStatement.setLong(1, id);


            preparedStatement.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(CommentDAOImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public Comment findComment(Long id) {
        Comment comment = new Comment();
        try {


            String find = "SELECT * FROM comment WHERE id=?";


            PreparedStatement preparedStatement = con.prepareStatement(find);


            preparedStatement.setLong(1, id);


            rs = preparedStatement.executeQuery();


            rs.next();
            comment.setId(rs.getLong(1));
            comment.setMatchId(rs.getLong(2));
            comment.setName(rs.getString(3));
            comment.setText(rs.getString(4));
            comment.setDate(rs.getTimestamp(5));


        } catch (SQLException ex) {
            Logger.getLogger(CommentDAOImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return comment;
    }

    @Override
    public List<Comment> getAllComments() {
        List<Comment> commentList = new ArrayList<Comment>();


        try {


            String getAll = "SELECT * FROM comment";


            PreparedStatement preparedStatement = con.prepareStatement(getAll);


            rs = preparedStatement.executeQuery();


            while (rs.next()) {
                Comment comment = new Comment();
                comment.setId(rs.getLong(1));
                comment.setMatchId(rs.getLong(2));
                comment.setName(rs.getString(3));
                comment.setText(rs.getString(4));
                comment.setDate(rs.getTimestamp(5));
                commentList.add(comment);


            }
        } catch (SQLException ex) {
            Logger.getLogger(CommentDAOImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return commentList;
    }

    @Override
    public List<Comment> getAllCommentsForMatch(Long matchId) {
        List<Comment> comments = new ArrayList<Comment>();
        try {


            String find = "SELECT * FROM comment WHERE match_id=?";


            PreparedStatement preparedStatement = con.prepareStatement(find);


            preparedStatement.setLong(1, matchId);


            rs = preparedStatement.executeQuery();


            while (rs.next()) {
                Comment comment = new Comment();
                comment.setId(rs.getLong(1));
                comment.setMatchId(rs.getLong(2));
                comment.setName(rs.getString(3));
                comment.setText(rs.getString(4));
                comment.setDate(rs.getTimestamp(5));
                comments.add(comment);
            }

        } catch (SQLException ex) {
            Logger.getLogger(CommentDAOImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return comments;
    }
}